<template>
  <view class="loading-state" :class="{ 'fullscreen': fullscreen }">
    <view class="loading-content">
      <!-- 加载动画 -->
      <view class="loading-spinner" v-if="type === 'spinner'">
        <view class="spinner-ring"></view>
      </view>
      
      <!-- 脉冲动画 -->
      <view class="loading-pulse" v-else-if="type === 'pulse'">
        <view class="pulse-dot" v-for="n in 3" :key="n"></view>
      </view>
      
      <!-- 骨架屏 -->
      <view class="loading-skeleton" v-else-if="type === 'skeleton'">
        <view class="skeleton-item" v-for="n in skeletonLines" :key="n"></view>
      </view>
      
      <!-- 默认图标 -->
      <view class="loading-icon" v-else>
        <text class="iconfont icon-loading"></text>
      </view>
      
      <!-- 加载文本 -->
      <view class="loading-text" v-if="text">{{ text }}</view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'LoadingState',
  props: {
    // 加载动画类型: spinner | pulse | skeleton | icon
    type: {
      type: String,
      default: 'spinner'
    },
    // 加载文本
    text: {
      type: String,
      default: '加载中...'
    },
    // 是否全屏显示
    fullscreen: {
      type: Boolean,
      default: false
    },
    // 骨架屏行数
    skeletonLines: {
      type: Number,
      default: 3
    }
  }
}
</script>

<style lang="scss" scoped>
.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 64rpx 32rpx;
  
  &.fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.9);
    z-index: 9999;
  }
}

.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24rpx;
}

// 旋转加载器
.loading-spinner {
  .spinner-ring {
    width: 64rpx;
    height: 64rpx;
    border: 6rpx solid #f0f0f0;
    border-top: 6rpx solid #1890ff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
}

// 脉冲加载器
.loading-pulse {
  display: flex;
  gap: 8rpx;
  
  .pulse-dot {
    width: 16rpx;
    height: 16rpx;
    background: #1890ff;
    border-radius: 50%;
    animation: pulse 1.4s ease-in-out infinite both;
    
    &:nth-child(1) {
      animation-delay: -0.32s;
    }
    
    &:nth-child(2) {
      animation-delay: -0.16s;
    }
  }
}

// 骨架屏
.loading-skeleton {
  width: 100%;
  max-width: 400rpx;
  
  .skeleton-item {
    height: 32rpx;
    background: linear-gradient(90deg, #f0f0f0 25%, #e6e6e6 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    border-radius: 8rpx;
    margin-bottom: 16rpx;
    animation: skeleton-loading 1.5s ease-in-out infinite;
    
    &:last-child {
      margin-bottom: 0;
      width: 60%;
    }
  }
}

// 图标加载器
.loading-icon {
  .iconfont {
    font-size: 64rpx;
    color: #1890ff;
    animation: spin 1s linear infinite;
  }
}

.loading-text {
  font-size: 28rpx;
  color: #666;
  text-align: center;
}

// 动画定义
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  0%, 80%, 100% {
    transform: scale(0);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes skeleton-loading {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}
</style>